package com.xiaobai.wechat.controller;


import com.alibaba.fastjson.JSON;
import com.xiaobai.wechat.request.MessageRequest;
import com.xiaobai.wechat.util.HttpUtil;
import com.xiaobai.wechat.util.JsoupUtil;
import com.xiaobai.wechat.util.WechatUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author feng
 */
@Slf4j
@RestController
public class MessageController {

    @Autowired
    private WechatUtil wechatUtil;

    @PostMapping(value = "message", consumes = {MediaType.TEXT_XML_VALUE}, produces = MediaType.TEXT_XML_VALUE)
    public Object message(@RequestBody MessageRequest messageRequest, HttpServletRequest request) {
        log.warn(JSON.toJSONString(messageRequest));
        String fromUserName = messageRequest.getFromUserName();
        String toUserName = messageRequest.getToUserName();
        messageRequest.setToUserName(fromUserName);
        messageRequest.setFromUserName(toUserName);
        messageRequest.setContent("啥玩意啊啊啊\n今日温度：001\n工作日");
        String ipAddress = HttpUtil.getIpAddress(request);
        String ipAddress1 = JsoupUtil.getIpAddress(ipAddress);
        System.out.println("ip:" + ipAddress);
        System.out.println("Address:" + ipAddress1);
        System.out.println(wechatUtil.getToken());
        return messageRequest;
    }

    @GetMapping(value = "message")
    public String message(@RequestParam("echostr") String echostr) {
        log.warn("echostr: {}", echostr);
        return echostr;
    }

    @GetMapping(value = "menuCreate")
    public String menuCreate() {
        return wechatUtil.menuCreate(" {\n"
                + "     \"button\":[\n"
                + "     {\n"
                + "          \"type\":\"click\",\n"
                + "          \"name\":\"天气\",\n"
                + "          \"key\":\"V1001_TODAY_WEATHER\"\n"
                + "      }\n"
                + "     ]\n"
                + " }");
    }
}
